/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.message.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.inventory.entity.StoreInventoryAlert;
import com.chlitina.store.modules.inventory.service.StoreInventoryAlertService;
import com.chlitina.store.modules.message.entity.StoreEmployeeTask;
import com.chlitina.store.modules.message.entity.SystemNotifyMessage;
import com.chlitina.store.modules.message.entity.SystemNotifyMessageDetail;
import com.chlitina.store.modules.message.service.StoreEmployeeTaskService;
import com.chlitina.store.modules.message.service.SystemNotifyMessageDetailService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Maps;

/**
 * 系统提示信息Controller
 * @author lvbo
 * @version 2013-07-16
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/message/systemNotifyMessage")
public class SystemNotifyMessageController extends BaseController {

	@Autowired
	private SystemNotifyMessageDetailService systemNotifyMessageDetailService;
	
	@Autowired
	private StoreEmployeeTaskService storeEmployeeTaskService;
	
	@Autowired
	private StoreInventoryAlertService storeInventoryAlertService;
	
	@ModelAttribute
	public SystemNotifyMessageDetail get() {
		return new SystemNotifyMessageDetail();
	}
	
	@RequestMapping(value = "companyMsg")
	public String companyMsg(SystemNotifyMessageDetail systemNotifyMessageDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		if (store != null){
			systemNotifyMessageDetail.setStore(store);
		}
		String currentSelectId = request.getParameter("currentSelectId");
		Page<SystemNotifyMessageDetail> page = systemNotifyMessageDetailService.find(new Page<SystemNotifyMessageDetail>(request, response), systemNotifyMessageDetail, SystemNotifyMessage.COMPANY_TYPE); 
		model.addAttribute("targetType", SystemNotifyMessage.COMPANY_TYPE);
		model.addAttribute("page", page);
        model.addAttribute("currentSelectId", currentSelectId);
		return "modules/message/systemNotifyMessageList";
	}

	@RequestMapping(value = "detail")
	public String form(@RequestParam("msgId") Long msgId, Model model) {
		SystemNotifyMessageDetail messageDetail = systemNotifyMessageDetailService.getMsgById(msgId);
		model.addAttribute("detail", messageDetail);
		int status = messageDetail.getStatus();
		if (status == SystemNotifyMessageDetail.UNREAD_STATUS) {
			systemNotifyMessageDetailService.changeStatus(msgId);
		}
		return "modules/message/systemNotifyMessageDetail";
	}

	@RequestMapping(value = "changeStatus")
	public String changeStatus(@RequestParam("msgId") Long msgId, Model model, RedirectAttributes redirectAttributes) {
		systemNotifyMessageDetailService.changeStatus(msgId);
		addMessage(redirectAttributes, "状态修改成功");
		return "redirect:"+Global.ADMIN_PATH+"/message/systemNotifyMessage/companyMsg";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		systemNotifyMessageDetailService.delete(id);
		addMessage(redirectAttributes, "删除信息成功");
		return "redirect:"+Global.ADMIN_PATH+"/message/systemNotifyMessage/companyMsg";
	}
	@ResponseBody
	@RequestMapping(value = "msgNumber")
	public String msgNumber(HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		Store store = StoreUtils.getStore();
		Long storeId = store.getId();
		Date date = new Date();
		Map<String, Integer> map = Maps.newHashMap();
		List<SystemNotifyMessageDetail> unreadSystemMsg = systemNotifyMessageDetailService.findUnreadMsg(storeId, date);
		List<StoreEmployeeTask> unreadTaskMsg = storeEmployeeTaskService.findUnreadMsg(storeId);
		List<StoreEmployeeTask> unfinishedMsg = storeEmployeeTaskService.findUnfinishedMsg(storeId);
		List<StoreInventoryAlert> needAlertStoreProduct = storeInventoryAlertService.findNeedAlertStoreProduct();
		if (needAlertStoreProduct != null) {
			map.put("alertStoreProduct", needAlertStoreProduct.size());
		}
		if (unreadSystemMsg != null) {
			map.put("unreadSysNumber", unreadSystemMsg.size());
		}
		if (unreadTaskMsg != null) {
			map.put("unreadTaskNumber", unreadTaskMsg.size());
		}
		if (unfinishedMsg != null) {
			map.put("unfinishedTaskNumber", unfinishedMsg.size());
		}
		return JsonMapper.getInstance().toJson(map);
	}

}
